package com.ldu.exam.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ldu.exam.model.ClaModel;
import com.ldu.exam.model.ClaModelForm;
import com.ldu.exam.service.ClaService;

@RequestMapping("/cla") 
@RestController
public class ClaContoller 
{
	@Autowired ClaService service;
	
	@GetMapping()
	public Page<ClaModel> findAll(@PageableDefault(size=20) Pageable page)
	{
		return this.service.findAll(page) ;
	}
	
	@GetMapping(value="/noPage")
	public Object findAllNoPage() 
	{
		return this.service.findAllNoPage();
	}
	
	@GetMapping(params="name")
	public Page<ClaModel> findByName(@RequestParam String name, 
			 @RequestParam( required=false, defaultValue="0")  int page,
			 @RequestParam( required=false, defaultValue="20") int size)
	{
		return this.service.findByName(name, page, size);
	}
	
	@PostMapping()
	public Object save( @RequestBody ClaModelForm model)
	{
		return this.service.save(model);
	}
	
	@PutMapping("/{id}")
	public Object update(@PathVariable String id, @RequestBody ClaModel model)
	{
		return this.service.update(id, model);
	}
	
	@GetMapping("/isExit")
	public Object isExit(@RequestParam String name) 
	{
		return this.service.isExit(name);
	}
	
	@DeleteMapping("/{id}")
	public Object delete(@PathVariable String id)
	{
		this.service.del(id);
		return ResponseEntity.noContent().build();
	}
}
